//
//  LRXSearchThingTableViewCell.m
//  路人侠
//
//  Created by 王涛 on 16/5/21.
//  Copyright © 2016年 zhangxulong. All rights reserved.
//

#import "LRXSearchThingTableViewCell.h"
#import "LRXThing.h"
#import "AppDelegate.h"
#import "LRXBuyThingCell.h"
#import "LRXBuyThingController.h"
#import "LRXLMeController.h"
@interface LRXSearchThingTableViewCell()
//cell属性
@property (nonatomic,weak) UIImageView * thingIcon;
@property (nonatomic,weak) UILabel * thingName;
@property (nonatomic,weak) UILabel * thingCount;
@property (nonatomic,weak) UILabel * thingMoney;
@property (nonatomic,weak) UIButton * jiaBtn;
@property (nonatomic,weak) UIButton * jianBtn;
@property (nonatomic,weak) UIView * pointView;
@property (nonatomic,strong) NSMutableArray * selectPointArray;
@end
@implementation LRXSearchThingTableViewCell

- (void)layoutSearchBuyThingCellSubView{
    
    buyThingArray = [NSMutableArray array];
    
    
    //共享变量
    //按钮的动态布局
    [self layoutBtn];
    
    UIImageView * thingIcon = [[UIImageView alloc]init];
    UILabel * thingInfo = [[UILabel alloc]init];
    UILabel * thingMoney = [[UILabel alloc]init];
    
    
    self.thingIcon = thingIcon;
    self.thingName = thingInfo;
    self.thingMoney = thingMoney;
    
    
    
    [self setSarchBuyThingCellModel];
    
    
    thingIcon.frame = CGRectMake(39 * wScale, 39 * hScale, 118 * hScale, 118 * hScale);
    thingInfo.frame = CGRectMake(198 * wScale, 53 * hScale, 305*wScale, 26*hScale);
    thingInfo.font = [UIFont systemFontOfSize:28 * hScale];
    [thingInfo sizeToFit];

    
    thingMoney.textAlignment = NSTextAlignmentLeft;
    thingMoney.font = [UIFont systemFontOfSize:28 * hScale];
    thingMoney.textColor = [UIColor colorWithRed:255 green:156 blue:0];
    thingMoney.frame = CGRectMake(199*wScale,121*hScale,117*wScale,29*hScale);
    
    
    
    
    [self addSubview:thingInfo];
    [self addSubview:thingIcon];
    [self addSubview:thingMoney];
    

}
/**
 *  请求到新的数据显示到cell中，通过全局静态变量buyThingArray 获取以前的值
 */
-(NSMutableArray *)selectPointArray
{
    if (_selectPointArray == nil) {
        _selectPointArray = [NSMutableArray array];
    }
    return _selectPointArray;
}

- (void)setSarchBuyThingCellModel
{
    //照片的设置
    NSString * imageStr;
    if (self.thingInfo.thingIcon && self.thingInfo.thingIcon.length > 0) {
        imageStr = [routeImageUrl stringByAppendingString:self.thingInfo.thingIcon];
    }
    
    //转UTF8 编码
    NSURL * imageUrl = [NSURL URLWithString: [imageStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    [self.thingIcon sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"de"]];
    
    NSString * selectCount;
    
    for (LRXThing * thing in [LRXBuyThingCell getSelectedArray]) {
        
        
       
        if ([thing.thingId isEqualToString:self.thingInfo.thingId]) {
            
            selectCount = thing.thingCount;
            
        }
    }
    
    //逻辑有问题
    if (!selectCount) {
        
        self.jianBtn.hidden = YES;
        
    }
    else
    {
        self.jianBtn.hidden = NO;
        self.thingCount.hidden = NO;
    }
    
    self.thingName.text =   [NSString stringWithFormat:@"%@(%@)",self.thingInfo.thingName,self.thingInfo.storeName];
    
    self.thingCount.text = selectCount;
    self.thingCount.textAlignment = NSTextAlignmentCenter;

    
    self.thingMoney.text = [self.thingInfo.thingMoney stringByAppendingString:@"元"];
    
}
- (void)layoutBtn
{
  
    
    UIButton * jianBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    UILabel * thingCount = [[UILabel alloc]init];
    UIButton * jiaBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    jianBtn.frame = CGRectMake(434*wScale, 110*hScale, 51 * hScale, 51 * hScale);
    
    thingCount.frame = CGRectMake(490 * wScale, 123 * hScale, 40 * wScale, 25 * hScale);
    thingCount.font = [UIFont systemFontOfSize:28 * hScale];
    thingCount.textAlignment = NSTextAlignmentCenter;
    
    
    jiaBtn.frame = CGRectMake(537*wScale,110*hScale, 51 * hScale,51 *hScale);
    if ([thingCount.text integerValue] <=0) {
        jianBtn.hidden = YES;
        thingCount.hidden = YES;
    }
    
    [jiaBtn setImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
    [jianBtn setImage:[UIImage imageNamed:@"jianhao"] forState:UIControlStateNormal];
    
    jiaBtn.tag = 1;
    jianBtn.tag = 0;
    
    [jiaBtn addTarget:self action:@selector(thingCountBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [jianBtn addTarget:self action:@selector(thingCountBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.jianBtn = jianBtn;
    self.jiaBtn = jiaBtn;
    self.thingCount = thingCount;
    
    [self addSubview:self.jianBtn];
    [self addSubview:self.jiaBtn];
    [self addSubview:self.thingCount];
    
}

- (void)thingCountBtnClick:(UIButton *)btn
{
 
    NSString * thingCount;
    //点击了加号
    
    if (btn.tag == 1) {
        thingCount = [NSString stringWithFormat:@"%ld",[self.thingCount.text integerValue] + 1];
        NSInteger flag = 0;
        for (LRXThing * thing in [LRXBuyThingCell getSelectedArray]) {
            //找到了thing 进行相应的操作
            if ([thing.thingId isEqualToString:self.thingInfo.thingId]) {
                
                if ([[LRXBuyThingCell getBuyThing] containsObject:thing]) {
                    thing.reChonoseAddCount ++;
                }
                flag = 1;
                thing.thingCount = thingCount;
                break;
            }
        }
        
        if (flag == 0) {
            
            self.thingInfo.thingCount = @"1";
            
            [[LRXBuyThingCell getSelectedArray] addObject:self.thingInfo];
        }
        
        CGPoint point = self.thingCount.center;
        point = [self convertPoint:point toView:[UIApplication sharedApplication].keyWindow];
        
        UIView * pointView = [[UIView alloc]init];
        pointView.frame = CGRectMake(0, 0, 20 * hScale, 20 * hScale);
        pointView.backgroundColor = [UIColor redColor];
        pointView.layer.cornerRadius = pointView.frame.size.width * 0.5;
        pointView.layer.masksToBounds = YES;
        
        [[UIApplication sharedApplication].keyWindow addSubview:pointView];
        
        [self.selectPointArray addObject:pointView];
        
        AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        CGMutablePathRef thePath = CGPathCreateMutable();
        CGPathMoveToPoint(thePath, NULL, point.x,point.y);
        CGPathAddQuadCurveToPoint(thePath, NULL,  point.x - 200 * wScale, point.y -100 * hScale, appDelegate.pointerCenter.x, appDelegate.pointerCenter.y);   //startPoint是起点，endPoint是终点，150，30是x,y轴的控制点，自行调整数值可以出现理想弧度效果
        bounceAnimation.path = thePath;
        bounceAnimation.removedOnCompletion=YES;
        bounceAnimation.fillMode=kCAFillModeForwards;
        bounceAnimation.duration = 0.4;
        bounceAnimation.delegate = self;
        pointView.center = appDelegate.pointerCenter;
        [pointView.layer addAnimation:bounceAnimation forKey:@"move"];
        
        
    }//点击了减号
    else{
        thingCount = [NSString stringWithFormat:@"%ld",[self.thingCount.text integerValue] - 1];
        NSInteger flag = 0;
        for (LRXThing * thing in [LRXBuyThingCell getSelectedArray]) {
            //找到了thing 进行相应的操作
            if ([thing.thingId isEqualToString:self.thingInfo.thingId]) {
                
                if ([[LRXBuyThingCell getSelectedArray] containsObject:thing]) {
                    thing.reChonoseSubCount ++;
                }
                flag = 1;
                NSInteger p = [thing.thingCount integerValue];
                thing.thingCount = [NSString stringWithFormat:@"%ld",--p];
           
                if (p <= 0) {
    
                    thing.thingCount = @"0";
                    self.thingCount.text = @"0";
                    

                    if ([[LRXBuyThingCell getBuyThing] containsObject:thing]) {
                        
                        [[LRXLMeController getSaveThingOfZero] addObject:thing];
                        
                    }
                    
                    
                    
                    [[LRXBuyThingCell getSelectedArray] removeObject:thing];
                }
                    break;
            }
        }
        if ([self.thingCellDelegate respondsToSelector:@selector(updateSearchThingRemind:)]) {
            
         
            [self.thingCellDelegate updateSearchThingRemind:[LRXBuyThingCell getSelectedArray]];
            
        }
    }
    if ([thingCount integerValue] <=0) {
        self.jianBtn.hidden = YES;
        self.thingCount.hidden = YES;
        
    }
    else
    {
        self.jianBtn.hidden = NO;
        self.thingCount.hidden = NO;
        self.thingCount.text = thingCount;
    }
    
    
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    if ([self.thingCellDelegate respondsToSelector:@selector(updateSearchThingRemind:)]) {
        
        [self.thingCellDelegate updateSearchThingRemind:[LRXBuyThingCell getSelectedArray]];
        
    }
    for(UIView * pointView in self.selectPointArray) {
        
        [pointView removeFromSuperview];
        
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
   
    [super setSelected:selected animated:animated];
    
}
-(void)setDelegate:(id)delegate{
    
    _thingCellDelegate = delegate;
}

@end
